//
//  UIViewController+IGNavigationItem.m
//  jingdongfang
//
//  Created by BOE on 2017/8/15.
//  Copyright © 2017年 kevinLY. All rights reserved.
//

#import "UIViewController+IGNavigationItem.h"

@implementation UIViewController (IGNavigationItem)

- (UIButton *)ig_setupLeftBackBarButtonItem
{
    UIButton *button = [self buttonWithTitle:nil
                                       image:[UIImage imageNamed:@"btn_back"]
                            highLightedImage:nil];
    [button addTarget:self
               action:@selector(ig_leftBackButtonPressed:)
     forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *spaceButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
    [spaceButton addTarget:self
                    action:@selector(ig_leftBackButtonPressed:)
          forControlEvents:UIControlEventTouchUpInside];
    [spaceButton addSubview:button];
    
    CGPoint center = button.center;
    center.y = spaceButton.center.y;
    button.center = center;
    
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithCustomView:spaceButton];
    
    self.navigationItem.leftBarButtonItem = spaceItem;
    return button;
}

- (void)ig_leftBackButtonPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (UIButton *)ig_setupLeftBarButtonItemWithTitle:(NSString *)title
                                           image:(UIImage *)image
                                highLightedImage:(UIImage *)highLightedImage
{
    UIButton *button = [self buttonWithTitle:title
                                       image:image
                            highLightedImage:highLightedImage];
    [button addTarget:self
               action:@selector(ig_leftButtonPressed:)
     forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    if (image) {
        spaceItem.width = 0;
    }
    UIBarButtonItem *btnItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItems = @[spaceItem, btnItem];
    return button;
}

- (void)ig_leftButtonPressed:(id)sender
{
    
}

- (UIButton *)ig_setupRightButtonItemWithTitle:(NSString *)title
                                         image:(UIImage *)image
                              highLightedImage:(UIImage *)highLightedImage
{
    UIButton *button = [self buttonWithTitle:title
                                       image:image
                            highLightedImage:highLightedImage];
    [button addTarget:self
               action:@selector(ig_rightButtonPressed:)
     forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    if (image) {
        spaceItem.width = 0;//-15
    }
    UIBarButtonItem *btnItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.rightBarButtonItems = @[spaceItem, btnItem];
    
    return button;
}

- (void)ig_rightButtonPressed:(id)sender
{
    
}

#pragma mark -

- (UIButton *)buttonWithTitle:(NSString *)title
                        image:(UIImage *)image
             highLightedImage:(UIImage *)highLightedImage
{
    UIButton *button = [[UIButton alloc] init];
    title = [title stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (title.length > 0) {
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor appColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14.f];
    }
    if (image) {
        if (!highLightedImage) {
            highLightedImage = image;
        }
        [button setImage:image forState:UIControlStateNormal];
        [button setImage:highLightedImage forState:UIControlStateHighlighted];
    }
    [button sizeToFit];
    button.titleLabel.adjustsFontSizeToFitWidth = YES;
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, -button.imageView.size.width, 0, button.imageView.size.width)];
    [button setImageEdgeInsets:UIEdgeInsetsMake(0, button.titleLabel.bounds.size.width, 0, -button.titleLabel.bounds.size.width)];
    return button;
}

@end
